package LeetCode._03_链表;

public class _05删除链表的倒数第N个节点 {
    public class ListNode {
       int val;
       ListNode next;
       ListNode() {}
       ListNode(int val) { this.val = val; }
       ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null) return null;
        int size = size(head);
        if(size == n) return head.next;
        ListNode tmp = head;
        for(int i=0;i<size-n-1;i++){
            tmp = tmp.next;
        }
        tmp.next = tmp.next.next;
        return head;
    }

    private int size(ListNode head){
        int size = 0;
        while(head!=null){
            size++;
            head = head.next;
        }
        return size;
    }
}
